package web;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;

public class JsonAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JSONArray result; // 返回的结果传到前台去

	
	@JSON(serialize=false)
	public JSONArray getResult() {
		return result;
	}

	public void setResult(JSONArray result) {
		this.result = result;
	}
     
	
	public String execute(){
		
		if(result!=null){
			return null;
		}
		
		try{
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("id", 1);
		jsonObject.put("text", "My Documents");
		jsonObject.put("state","closed");
		JSONArray children = new JSONArray();
		for(int i=2;i<4;i++){
			JSONObject child = new JSONObject();
			child.put("id", i);
			child.put("text", "Photos"+i);
			child.put("state","closed");
			JSONArray children2 = new JSONArray();
			JSONObject child2 = new JSONObject();
			child2.put("id", 2+i);
			child2.put("text", "Photos"+2+i);
			child2.put("state","closed");
			children2.add(child2);
			child.put("children", children2);
			children.add(child);
		}
		jsonObject.put("children", children);
		JSONArray jsonArray = new JSONArray(); // json的数组
		jsonArray.add(jsonObject);
		this.result = jsonArray;
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
		
	}
	
	
	
}
